package com.gexin.model.order;

import com.alibaba.fastjson.JSONObject;
import com.gexin.common.cache.CacheUtils;
import com.gexin.common.cache.RedisKeyConst;
import com.gexin.controller.commom.ExpressApiController;
import com.gexin.controller.commom.ExpressController;
import com.gexin.model.order.base.BaseOrderExpress;
import com.jfinal.kit.LogKit;
import com.jfinal.log.Log;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * 业务名称:  t_order_express(订单的物流记录)
 */
@SuppressWarnings("serial")
public class OrderExpress extends BaseOrderExpress<OrderExpress> {
	public static final OrderExpress dao = new OrderExpress();

	private  static  final Log log =Log.getLog(OrderExpress.class);
	/**
	 * fang  2016-06-28
	 * 根据order_id 查找
	 *
	 */

	public OrderExpress getOrderExpressByOrderId(String order_id, Integer type){
		String sql=" select *  from t_order_express where order_id=? and type=?";
		return dao.findFirst(sql,order_id,type);
	}

	/**
	 * fang  2016-06-28
	 * 根据order_id 查找  已经签收的
	 *
	 */

	public OrderExpress getAlreadyOrderExpressByOrderId(String order_id){
		String sql=" select *  from t_order_express where order_id=?  and express_state=4 ";
		return dao.findFirst(sql,order_id);
	}

	/**
	 * 黎峻
	 * 商品订单的物流信息
	 */
	public OrderExpress cache_getProductOrderExpressByOrderId(String order_id){
		Order order =Order.dao.findById(order_id);
		if(order==null || StringUtils.isEmpty(order.getId())){
			log.error("商品订单传入的order_id："+order_id+"，在数据库未找到！");
			return null;
		}
		if(StringUtils.isEmpty(order.getExpressName()) || StringUtils.isEmpty(order.getExpressNo())){
			return null;
		}
		OrderExpress orderExpress = CacheUtils.get(RedisKeyConst.Product_Order_Express+order.getId());
		if(orderExpress==null){
			if(OrderExpress.dao.getOrderExpressByOrderId(order.getId(),0)==null){
				JSONObject expressObject= ExpressController.ajax_search(order.getExpressName(),order.getExpressNo());
				String content=expressObject.getString("data");
				String status=expressObject.getString("status");
				OrderExpress orderExpress2=new OrderExpress();
				orderExpress2.setContent(content);
				orderExpress2.setExpressState(Integer.parseInt(status));
				orderExpress2.setOrderId(order_id);
				orderExpress2.setUpdateTime(new Date());
				orderExpress2.save();
				orderExpress=orderExpress2;
				CacheUtils.set(RedisKeyConst.Product_Order_Express+order.getId(), orderExpress,1800);
			}
			else{
				if(OrderExpress.dao.getAlreadyOrderExpressByOrderId(order_id)!=null){
					orderExpress=OrderExpress.dao.getAlreadyOrderExpressByOrderId(order_id);
					CacheUtils.set(RedisKeyConst.Product_Order_Express+order.getId(), orderExpress,1800);
				}else{
					JSONObject expressObject=ExpressController.ajax_search(order.getExpressName(),order.getExpressNo());
					String content=expressObject.getString("data");
					String status=expressObject.getString("status");
					OrderExpress orderExpress2=OrderExpress.dao.getOrderExpressByOrderId(order.getId(),0);
					orderExpress2.setContent(content);
					orderExpress2.setExpressState(Integer.parseInt(status));
					orderExpress2.setOrderId(order_id);
					orderExpress2.setUpdateTime(new Date());
					orderExpress2.update();
					orderExpress=orderExpress2;
					CacheUtils.set(RedisKeyConst.Product_Order_Express+order.getId(), orderExpress,1800);
				}
			}
		}
		return orderExpress;
	}

	/**
	 * getExpressByOrderId
	 * 缓存
	 */
	public OrderExpress cache_getORderExpressByOrderId(String order_id){
		Order order= Order.dao.findByIdLoadColumns(order_id,"id,express_name,express_no");
		if(order==null){
			return null;
		}
		OrderExpress orderExpress = CacheUtils.get(RedisKeyConst.Order_Express+order.getId());
		//orderExpress=null;
		if(orderExpress==null){
			if(OrderExpress.dao.getOrderExpressByOrderId(order.getId(),0)==null){
				JSONObject expressObject= ExpressController.ajax_search(order.getExpressName(),order.getExpressNo());
				String content=expressObject.getString("data");
				String status=expressObject.getString("status");
				OrderExpress orderExpress2=new OrderExpress();
				orderExpress2.setContent(content);
				orderExpress2.setExpressState(Integer.parseInt(status));
				orderExpress2.setOrderId(order_id);
				orderExpress2.setUpdateTime(new Date());
				orderExpress2.save();
				orderExpress=orderExpress2;
				CacheUtils.set(RedisKeyConst.Order_Express+order.getId(), orderExpress,1800);
			}
			else{
				if(OrderExpress.dao.getAlreadyOrderExpressByOrderId(order_id)!=null){
					orderExpress= OrderExpress.dao.getAlreadyOrderExpressByOrderId(order_id);
					CacheUtils.set(RedisKeyConst.Order_Express+order.getId(), orderExpress,1800);
				}else{
					JSONObject expressObject= ExpressController.ajax_search(order.getExpressName(),order.getExpressNo());
					String content=expressObject.getString("data");
					String status=expressObject.getString("status");
					OrderExpress orderExpress2= OrderExpress.dao.getOrderExpressByOrderId(order.getId(),0);
					orderExpress2.setContent(content);
					orderExpress2.setExpressState(Integer.parseInt(status));
					orderExpress2.setOrderId(order_id);
					orderExpress2.setUpdateTime(new Date());
					orderExpress2.update();
					orderExpress=orderExpress2;
					CacheUtils.set(RedisKeyConst.Order_Express+order.getId(), orderExpress,1800);
				}
			}
		}

		return orderExpress;
	}
	/**
	 * 物流信息更新
	 */
	public void express_update(){
		String back = ExpressApiController.search(getExpressCom(), getExpressNo());
		JSONObject obj = JSONObject.parseObject(back);
		if (obj.getString("code").equals("10000")) {
			JSONObject result = obj.getJSONObject("result");
			if (result.getString("showapi_res_code").equals("0")) {
				JSONObject body = result.getJSONObject("showapi_res_body");
				int status = body.getIntValue("status");
				setExpressPhone(body.getString("tel"));
				setExpressState(status);
				setContent(body.getString("data"));
				setUpdateTime(new Date());
				if (status<=1) {
					LogKit.info("快递数据查询失败："+body.getString("msg"));
				}
				update();
			}else {
				//数据提供方状态异常
				LogKit.info("快递数据查询异常："+result.getString("showapi_res_error"));
			}
		}else {
			//接口方状态异常
			LogKit.info("快递查询接口调用失败："+obj.getString("msg"));
		}
	}

}
